home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac: Not for Sale / Another.not.for.sale (Australia).iso / hold me in your arms / Nexus / nexus.software / magic-money.server < prev    next >
Internet Message Format  |  1994-05-22  |  8KB

  1. From: Sameer <sameer@soda.berkeley.edu>
  2. Subject: Magic Money Client/Server using IPC
  3. To: cypherpunks@toad.com
  4. Date: Sat, 14 May 1994 23:44:37 -0700 (PDT)
  5. Cc: nexus-gaia@netcom.com
  6.  
  7.     As I posted only a few hours ago, I have been working on
  8. improving the Magic Money interface. I've written a server application
  9. for the Magic Money server which allows the server to sit on a port
  10. and wait for connections. When a connection comes in it forks and
  11. processes the request, by just taking the input, passing it to the 's'
  12. server released by Pr0duct Cypher, and then returning the server's
  13. output.
  14.  
  15.     The client is a front end to Pr0duct Cypher's 'c' program,
  16. which handles the communication between the 'c' client and the server
  17. running on a socket. I have written the client so that it can be run
  18. from any directory, but it looks in ~/.bank for the bank.asc,
  19. rand.dat, and other files that the program uses.
  20.  
  21.     I just wrote this code today, so I'm sure it lacks many safety
  22. checks. If you'd like to point out where it goes wrong, I'd appreciate
  23. it greatly.
  24.     
  25. To invoke the server:
  26.  
  27. Edit server.pl and give it the port number you want.
  28.  
  29. Run 'server.pl' in the directory which has the 's' program and the files that
  30. the 's' program uses. server.pl will fork and wait on the port specified.
  31.  
  32. To run the client:
  33.  
  34. Create the ~/.bank directory, and put rand.dat and bank.asc in that
  35. directory.
  36.  
  37. Edit the client.pl to reflect the port number and the hostname of the
  38. server, as well as the location of Pr0duct Cypher's 'c' binary.
  39.  
  40. client.pl -initialize
  41.  
  42.     Generates your account.
  43.  
  44. client.pl -incoming [filename]
  45.  
  46.     Takes in incoming coins (which someone has given you) either from
  47. filename or stdin (if the filename argument is missing) and adds their
  48. value to your wallet. (Doing the communication with the server that is
  49. necessary)
  50.  
  51. client.pl -extract [filename]
  52.  
  53.     Extract coins that you own into filename, or if filename
  54. doesn't exist pgp ascii-armor the coins and send them to stdout.
  55.  
  56. client.pl -exchange
  57.  
  58.     Exchange your old coins for new ones.
  59.  
  60.  
  61. server.pl:
  62. #!/usr/local/bin/perl
  63. # Perl script to attach a Magic Money Server to a port
  64. # Sameer <sameer@soda.berkeley.edu>
  65.  
  66.  
  67. ($port) = @ARGV;
  68. $port = 1992 unless $port;
  69. $magicserver = "s";
  70. $waitlock = "waiter.pid";
  71. $processlock = "processor.pid";
  72.  
  73. require 'sys/socket.ph';
  74. require './shlock.pl';
  75.  
  76. # First check to see if the process is running
  77. unless(&shlock($waitlock))
  78. {
  79.     print "Process already running\n";
  80.     exit;
  81. }
  82.  
  83. $sockaddr = 'S n a4 x8';
  84.  
  85. ($name, $aliases, $proto) = getprotobyname('tcp');
  86. ($name, $aliases, $port) = getservbyname($port, 'tcp')
  87.     unless $port =~ /^\d+$/;
  88.  
  89. $this = pack($sockaddr, &AF_INET, $port, "\0\0\0\0");
  90.  
  91. select(NS); $| = 1; select(stdout);
  92.  
  93. socket(S, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!";
  94. bind(S, $this) || die "bind: $!";
  95. listen(S, 5) || die "connect: $!";
  96.  
  97. select(S); $| = 1; select(stdout);
  98.  
  99. # Ok the socket has been setup. Fork, wait for the parent lock to die
  100. # and then lock again
  101. if(fork)
  102. {
  103.     exit;
  104. }
  105.  
  106. # Wait for the old process to die
  107. sleep 10 unless(&shlock($waitlock));
  108.  
  109. for (;;) {
  110. #    print "Listening again\n";
  111.     ($addr = accept(NS,S)) || die $!;
  112.     unless(fork)
  113.     {
  114. #        print "accept ok\n";
  115.     ($af,$port,$inetaddr) = unpack($sockaddr,$addr);
  116.     @inetaddr = unpack('C4',$inetaddr);
  117. #    print "$af $port @inetaddr\n";
  118.  
  119.     $tmpin = "/tmp/mmin." . $$ ;
  120.     $tmpout = "/tmp/mmout." . $$ ;
  121.  
  122.     open(TIN, ">$tmpin") || die $!;
  123.  
  124.     print NS "Magic Money Bank: " . $bank . "\n";
  125.     print NS "Feed server\n";
  126.     while (<NS>) {
  127.         print TIN;
  128.         last if /^-----END/ ;
  129.     }
  130.     close(TIN);
  131.  
  132.     # Wait for the process lock to stop
  133.  
  134.     unless(&shlock($processlock))
  135.     {
  136.         print NS "Please wait for other requests to finish.";
  137.         do
  138.         {
  139.         print NS "." ;
  140.         sleep 10;
  141.         }
  142.         until(&shlock($processlock));
  143.     }
  144.     
  145.     print NS "order processing.";
  146.  
  147.     # Run magic money
  148.     open(MM, "| $magicserver > $tmpout");
  149.     open(TIN, $tmpin);
  150.     while(<TIN>)
  151.     {
  152.         print NS "." ;
  153.         print MM;
  154.     }
  155.     close(TIN);
  156.     close(MM);
  157.  
  158.     print NS "done.\nServer response\n";
  159.     open(OUTPUT, $tmpout);
  160.     print NS <OUTPUT>;
  161.     close OUTPUT;
  162.     exit;
  163.     }
  164. }
  165.  
  166.  
  167. client.pl:
  168. #!/usr/local/bin/perl
  169. # Perl script to make dealing with the magic money oh so much easier
  170. # Sameer <sameer@soda.berkeley.edu>
  171.  
  172. require 'sys/socket.ph';
  173.  
  174. $mmclient = "/usr/local/bin/mmclient" ;
  175. $pgp = "/usr/local/bin/pgp" ;
  176. $port = 1992;
  177. $host = "localhost";
  178.  
  179. sub connectgrab
  180. {
  181.     local($them, $port, $infile, $outfile) = @_;
  182.  
  183.     $sockaddr = 'S n a4 x8';
  184.     chop($hostname = `hostname`);
  185.  
  186.     ($name, $aliases, $proto) = getprotobyname('tcp');
  187.     ($name, $aliases, $port) = getservbyname($port, 'tcp')
  188.     unless $port =~ /^\d+$/;
  189.     ($name, $aliases, $type, $len, $thisaddr) =
  190.     gethostbyname($hostname);
  191.     ($name, $aliases, $type, $len, $thataddr) = gethostbyname($them);
  192.     
  193.     $this = pack($sockaddr, &AF_INET, 0, $thisaddr);
  194.     $that = pack($sockaddr, &AF_INET, $port, $thataddr);
  195.     
  196.     socket(S, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!";
  197.     bind(S, $this) || die "bind: $!";
  198.     connect(S, $that) || die "connect: $!";
  199.     
  200.     select(S); $| = 1; select(stdout);
  201.     
  202.     # Wait until we get the prompt to start
  203.     while(<S>)
  204.     {
  205.     last if /^Feed server$/ ;
  206.     }
  207.    
  208.     # Send the stuff to the server
  209.     print "Sending to server.\n";
  210.     open(INPUT, $infile) || die "can't open $infile: $!";
  211.     while(<INPUT>)
  212.     {
  213.     print S;
  214.     }
  215.     close INPUT;
  216.  
  217.     # Wait for the server to finish processing.. tell the user it is processing
  218.     print "Waiting for server to process.\n";
  219.     while(<S>)
  220.     {
  221.     last if /^Server response$/;
  222.     }
  223.  
  224.     # Now grab the server's response
  225.     open(OUTPUT, "> $outfile") || die "can't open $outfile: $!";
  226.     while(<S>)
  227.     {
  228.     print OUTPUT;
  229.     }
  230.     close(OUTPUT);
  231.  
  232.     close S;
  233.     print "Finished with server.\n";
  234. }
  235.  
  236. ## Main
  237. ## Deal with user requests
  238.  
  239.  
  240. # Process incoming money
  241.  
  242. sub processincoming
  243. {
  244.     if($ARGV[0] ne '-')
  245.     {
  246.     $ARGV[0] = &expandfile($ARGV[0]);
  247.     }
  248.     open(FILE, "> temp.dat") || die "can't create temp.dat: $!";
  249.     print FILE <>;
  250.     close FILE;
  251.  
  252.     system("$mmclient temp.dat");
  253.     unlink("temp.dat");
  254.     &deal;
  255. }
  256.  
  257. # Initialize client
  258.  
  259. sub initialize
  260. {
  261.     system("$mmclient -i");
  262.     &deal;
  263. }
  264.  
  265. # Exchange coins
  266.  
  267. sub exchangecoins
  268. {
  269.     system("$mmclient -x");
  270.     &deal;
  271. }
  272.  
  273. sub deal
  274. {
  275.     &connectgrab($host, $port, "output.asc", "serverreply.asc");
  276.  
  277.     system("$mmclient serverreply.asc");
  278. #    unlink("serverreply.asc");
  279. #    unlink("output.asc");
  280. }
  281.  
  282. sub extractcoins
  283. {
  284.  
  285. #    if($ARGV[0] eq '-')
  286. #    {
  287. #    # Error
  288. #    print "Must specify a filename to extract coins to\n";
  289. #    exit;
  290. #    }
  291.  
  292.     if($ARGV[0] ne '-')
  293.     {
  294.     $file = &expandfile($ARGV[0]);
  295.     
  296.     if( -e $file )
  297.     {
  298.         # Error
  299.         print "File already exists\n";
  300.         exit;
  301.     }
  302.     # Check if the file can be made
  303.     open(FILE, "> $file") || die "Can't create $file: $!";
  304.     close FILE;
  305.     unlink($file);
  306.     }
  307.  
  308.     system("$mmclient -p");
  309.     # Now move coins.dat away so that another extraction doesn't mean money
  310.     # is lost
  311.  
  312.     # Send it to another file or stdout
  313.  
  314.     if($file)
  315.     {
  316.     rename("coins.dat", $file);
  317.     print "Coins moved to $file\n";
  318.     }
  319.     else
  320.     {
  321.     print "Coins going out, ascii armored.\n";
  322.     open(COINSDAT, "coins.dat");
  323.     open(ASCII, "| $pgp -af 2>/dev/null");
  324.     print ASCII <COINSDAT>;
  325.     close ASCII;
  326.     close COINSDAT;
  327. #    unlink("coins.dat");
  328.     }
  329. }
  330.  
  331. sub expandfile
  332. {
  333.     # If a file has a leading / don't add the startdir
  334.     # otherwise prepend $startdir
  335.  
  336.     local($fname) = @_;
  337.     if(index($fname, '/') == 0)
  338.     {
  339.     return($fname);
  340.     }
  341.     else
  342.     {
  343.     return($startdir . "/" . $fname);
  344.     }
  345. }
  346.  
  347. #########
  348. # THE MAIN
  349. #########
  350.  
  351.  
  352. # This bit of the program takes the cmdline arguments, etc.
  353. $startdir = $ENV{'PWD'};
  354. chdir($ENV{'HOME'} . "/.bank") || die "can't chdir to ~/.bank: $!";
  355.  
  356. $command = $ARGV[0];
  357. shift;
  358.  
  359. unless($ARGV[0])
  360. {
  361.     unshift(ARGV, '-');
  362. }
  363.  
  364.  
  365. &processincoming if $command eq '-incoming';
  366. &initialize if $command eq '-initialize';
  367. &exchangecoins if $command eq '-exchange';
  368. &extractcoins if $command eq '-extract';
  369.  
  370.